package com.todo.biz.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.convert.BizBaseConvert;
import com.todo.biz.application.entity.bo.BizAssociateBO;
import com.todo.biz.application.entity.context.SimpleBusinessDataContext;
import com.todo.biz.application.frontend.service.BizAssociateAggService;
import com.todo.biz.application.handler.bizData.AbstractBusinessDataHandle;
import com.todo.biz.application.handler.bizData.BusinessDataFactory;
import com.todo.biz.application.service.BizAssociateService;
import com.todo.biz.common.enums.AssociateRelationTypeEnum;
import com.todo.biz.iface.frontend.request.*;
import com.todo.biz.iface.frontend.response.CollectBusinessDataVO;
import com.todo.biz.iface.frontend.response.LikeBusinessDataVO;
import com.todo.common.common.entity.request.PageRequest;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.TodoCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (BizAssociate)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:41
 */
@Slf4j
@Service("bizAssociateAggService")
public class BizAssociateAggServiceImpl implements BizAssociateAggService {

    @Resource
    private BizAssociateService associateService;
    @Resource
    private BusinessDataFactory businessDataFactory;

    @Override
    public BizResponse<Boolean> likeBusinessData(LikeBusinessDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        BizAssociateBO bizAssociateBO = new BizAssociateBO();
        bizAssociateBO.setId(TodoCommonUtils.buildLongGuid());
        bizAssociateBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()));
        bizAssociateBO.setStatus(1);
        bizAssociateBO.setUserId(param.get_user().getUserId());
        bizAssociateBO.setRelationType(AssociateRelationTypeEnum.LIKE);
        bizAssociateBO.setRelationId(Long.valueOf(param.getRelationId()));
        boolean likeResult = associateService.insertBizAssociate(bizAssociateBO);
        return BizResponse.success(likeResult);
    }

    @Override
    public BizResponse<Boolean> collectBusinessData(CollectBusinessDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        BizAssociateBO bizAssociateBO = new BizAssociateBO();
        bizAssociateBO.setId(TodoCommonUtils.buildLongGuid());
        bizAssociateBO.setBusinessScene(BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()));
        bizAssociateBO.setStatus(1);
        bizAssociateBO.setUserId(param.get_user().getUserId());
        bizAssociateBO.setRelationType(AssociateRelationTypeEnum.COLLECT);
        bizAssociateBO.setRelationId(Long.valueOf(param.getRelationId()));
        boolean collectResult = associateService.insertBizAssociate(bizAssociateBO);
        return BizResponse.success(collectResult);
    }

    @Override
    public BizResponse<Boolean> removeLikeData(RemoveLikeDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<Long> collect = param.getLikeIdList().stream().map(Long::valueOf).collect(Collectors.toList());
        boolean removeResult = associateService.removeBizAssociateByIdList(param.get_user().getUserId(), collect);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<Boolean> removeCollectData(RemoveCollectDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<Long> collect = param.getCollectIdList().stream().map(Long::valueOf).collect(Collectors.toList());
        boolean removeResult = associateService.removeBizAssociateByIdList(param.get_user().getUserId(), collect);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizPageResponse<LikeBusinessDataVO> queryLikeBusinessData(QueryLikeBusinessDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageIndex(param.getPageIndex());
        pageRequest.setPageSize(param.getPageSize());
        pageRequest.setOrderByColumn(param.getOrderByColumn());
        pageRequest.setIsAsc(param.getIsAsc());
        pageRequest.setCurrentIndex(param.getCurrentIndex());
        PageResult<BizAssociateBO> pageResult = associateService.queryBizAssociateByPage(
                param.get_user().getUserId(),
                BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()),
                AssociateRelationTypeEnum.LIKE,
                pageRequest);
        if (pageResult == null) {
            return BizPageResponse.success();
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return BizPageResponse.success(PageResult.of(null, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal()));
        }
        List<Long> relationIdList = pageResult.getResults().stream().map(BizAssociateBO::getRelationId).collect(Collectors.toList());
        AbstractBusinessDataHandle businessDataHandle = businessDataFactory.getBusinessDataHandle(BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()));
        if (businessDataHandle == null) {
            log.error("BizAssociateAggService#queryLikeBusinessData 当前业务场景无可关联数据，操作异常 param：【{}】", JSONObject.toJSONString(param));
            return BizPageResponse.success(PageResult.of(null, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal()));
        }
        Map<Long/*bizId*/, SimpleBusinessDataContext> bizDataMap = businessDataHandle.querySimpleBizData(relationIdList);
        return BizPageResponse.success(BizBaseConvert.buildLikeBusinessDataVOList(pageResult, bizDataMap));
    }

    @Override
    public BizPageResponse<CollectBusinessDataVO> queryCollectBusinessData(QueryCollectBusinessDataParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageIndex(param.getPageIndex());
        pageRequest.setPageSize(param.getPageSize());
        pageRequest.setOrderByColumn(param.getOrderByColumn());
        pageRequest.setIsAsc(param.getIsAsc());
        pageRequest.setCurrentIndex(param.getCurrentIndex());
        PageResult<BizAssociateBO> pageResult = associateService.queryBizAssociateByPage(
                param.get_user().getUserId(),
                BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()),
                AssociateRelationTypeEnum.COLLECT,
                pageRequest
        );
        if (pageResult == null) {
            return BizPageResponse.success();
        }
        if (CollectionUtils.isEmpty(pageResult.getResults())) {
            return BizPageResponse.success(PageResult.of(null, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal()));
        }
        List<Long> relationIdList = pageResult.getResults().stream().map(BizAssociateBO::getRelationId).collect(Collectors.toList());
        AbstractBusinessDataHandle businessDataHandle = businessDataFactory.getBusinessDataHandle(BusinessSceneEnum.getBusinessSceneEnumByScene(param.getBusinessScene()));
        if (businessDataHandle == null) {
            log.error("BizAssociateAggService#queryCollectBusinessData 当前业务场景无可关联数据，操作异常 param：【{}】", JSONObject.toJSONString(param));
            return BizPageResponse.success(PageResult.of(null, pageResult.getPageNum(), pageResult.getPageSize(), pageResult.getTotal()));
        }
        Map<Long, SimpleBusinessDataContext> bizDataMap = businessDataHandle.querySimpleBizData(relationIdList);
        return BizPageResponse.success(BizBaseConvert.buildCollectBusinessDataVOList(pageResult, bizDataMap));
    }
}
